package com.lky.swagger2pro.service.impl;

import com.lky.swagger2pro.mapper.BookMapper;
import com.lky.swagger2pro.model.Book;
import com.lky.swagger2pro.service.IBookService;
import com.lky.swagger2pro.util.JsonResponseBody;
import com.lky.swagger2pro.util.ResponseStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BookServiceImpl implements IBookService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public JsonResponseBody<?> deleteByPrimaryKey(String bookid) {
        bookMapper.deleteByPrimaryKey(bookid);
        return new JsonResponseBody<>();
    }

    @Override
    public JsonResponseBody<?> insert(Book record) {
        int i = bookMapper.insert(record);
        if(i<1)
            return new JsonResponseBody<>(ResponseStatus.STATUS_201);
        return new JsonResponseBody<>();
    }

    @Override
    public JsonResponseBody<Book> selectByPrimaryKey(String bookid) {
        Book book = bookMapper.selectByPrimaryKey(bookid);
        if(null==book)
            return new JsonResponseBody<>(ResponseStatus.STATUS_202);
        return new JsonResponseBody<>(book);
    }

    @Override
    public JsonResponseBody<?> updateByPrimaryKey(Book record) {
        int i = bookMapper.updateByPrimaryKey(record);
        if(i<1)
            return new JsonResponseBody<>(ResponseStatus.STATUS_201);
        return new JsonResponseBody<>();
    }

    @Override
    public JsonResponseBody<List<Book>> queryAll() {
        List<Book> books = bookMapper.queryAll();
        if(null==books)
            return new JsonResponseBody<>(ResponseStatus.STATUS_202);
        return new JsonResponseBody<>(books);
    }

    @Override
    public JsonResponseBody<Map<String, Object>> queryMap() {
        Map<String,Object> json=new HashMap<String,Object>();
        json.put("name","张三");
        json.put("age",21);
        json.put("sex","男");
        json.put("phone","15874024702");
        return new JsonResponseBody<>(json);
    }
}
